package org.litu.app.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.litu.app.service.ISysRoleMenuService;
import org.litu.base.log.LtLog;
import org.litu.base.log.LtLogOperation;
import org.litu.core.base.ApiRes;
import org.litu.core.base.BaseController;
import org.litu.core.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色菜单管理controller
 *
 * @author yueye
 */
@RestController
@LtLog(module = "权限角色表")
@RequestMapping("/system/sysRoleMenu")
@Api(value = "权限角色表", tags = {"权限角色表"}, protocols = "http,https")
public class SysRoleMenuController extends BaseController {

    @Autowired
    private ISysRoleMenuService sysRolemenuService;

    /**
     * 获取角色的所有菜单ID信息
     *
     * @param roleId 角色ID
     * @return 保存成功
     */
    @GetMapping(value = "/roleMenus")
    @ApiOperation(value = "获取角色的全部菜单信息", notes = "根据角色ID返回角色的所有权限的ID列表！")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    public ApiRes roleMenus(@RequestParam(required = true) String roleId) {
        if (StringUtils.isBlank(roleId)) {
            return ApiRes.error(ResultEnum.ParamError, "roleId不能为空！");
        }
        List<String> menuIds = sysRolemenuService.roleMenuIds(roleId);
        return ApiRes.ok(menuIds);
    }

    /**
     * 保存角色的权限
     *
     * @param roleId  角色ID
     * @param menuIds 菜单IDs
     * @return 保存成功
     */
    @PostMapping(value = "/saveRoleMenus")
    @LtLogOperation(operationEx = "角色授权")
    @ApiOperation(value = "保存角色的权限信息", notes = "对于角色的权限信息进行保存，然后权限通过数组进行传输即可。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(name = "menuIds", value = "菜单IDs", required = true, allowMultiple = true, dataType = "String")
    })
    public ApiRes saveRoleMenus(String roleId, String[] menuIds) {
        if (StringUtils.isBlank(roleId)) {
            return ApiRes.error(ResultEnum.ParamError, "roleId不能为空！");
        }

        if (menuIds.length > 0 && StringUtils.isAnyBlank(menuIds)) {
            return ApiRes.error(ResultEnum.ParamError, "menuIds参数包含空值！");
        }
        boolean res = sysRolemenuService.save(roleId, menuIds);
        return res ? ApiRes.ok("保存成功!") : ApiRes.error(ResultEnum.SaveError);
    }
}
